package csvfilereader;

import java.io.*;
import java.util.*;
import salesorder.*;
import au.com.bytecode.opencsv.*;

public class CSVFileReader {

    private static int purchaseOrderNumberIndex;
    private static int salesOrderNumberIndex;
    private static int shipmentIdIndex;
    private static int partNumberIndex;
    private static int displayNameIndex;
    private static int quantityIndex;
    private static int invoiceIdIndex;
    private static int grossWeightIndex;
    private static int serialNumberIndex;
    private static int boxNumberIndex;
    private List<SalesOrder> salesOrders;
    
    public List<SalesOrder> getSalesOrders() {
        return salesOrders;
    }

    public void setSalesOrders(List<SalesOrder> salesOrders) {
        this.salesOrders = salesOrders;
    }

    
    public static List<SalesOrder> parseFile(byte[] bytes) {
        CSVFileReader csvFileReader = new CSVFileReader();
        List<SalesOrderLine> salesOrderLines = new ArrayList<SalesOrderLine>();
        

        try {
            CSVReader csvReader = new CSVReader(new InputStreamReader(new ByteArrayInputStream(bytes), "UTF16"), '\t');
            List<String[]> content = csvReader.readAll();

            purchaseOrderNumberIndex = CSVFileReader.defineIndex(content.get(3), "PO");
            salesOrderNumberIndex = CSVFileReader.defineIndex(content.get(3), "Sales Doc");
            shipmentIdIndex = CSVFileReader.defineIndex(content.get(3), "Delivery#");
            partNumberIndex = CSVFileReader.defineIndex(content.get(3), "Material");
            displayNameIndex = CSVFileReader.defineIndex(content.get(3), "Prod Desc");
            quantityIndex = CSVFileReader.defineIndex(content.get(3), "  Ship-Qty");
            invoiceIdIndex = CSVFileReader.defineIndex(content.get(3), "Invoice No");
            grossWeightIndex = CSVFileReader.defineIndex(content.get(3), " Gross weig");
            serialNumberIndex = CSVFileReader.defineIndex(content.get(3), "Serial#");
            boxNumberIndex = CSVFileReader.defineIndex(content.get(3), "SSCC");
            
            for (int i = 4; i < content.size() - 1; i++) {
                if (content.get(i).length > 1) {
                    SalesOrderLine soLine = new SalesOrderLine(content.get(i)[purchaseOrderNumberIndex], content.get(i)[salesOrderNumberIndex],
                                                               content.get(i)[shipmentIdIndex], content.get(i)[partNumberIndex],
                                                               content.get(i)[displayNameIndex], (int)Double.parseDouble(content.get(i)[quantityIndex].trim()),
                                                               content.get(i)[invoiceIdIndex], (int) (Double.parseDouble(content.get(i)[grossWeightIndex].trim()) * 10000),
                                                               content.get(i)[serialNumberIndex], content.get(i)[boxNumberIndex]);
                    if (!soLine.getSerialNumber().equals("")) {
                        soLine.getSerialNumbers().add(soLine.getSerialNumber());
                    }
                    soLine.getBoxNumbers().add(soLine.getBoxNumber());
                    salesOrderLines.add(soLine);
                }
            }
            

 
            for (int i = 0; i < salesOrderLines.size(); i++) {
                for (int j = salesOrderLines.size() - 1; j > i; j--) {
                    int result = CSVFileReader.compare(salesOrderLines.get(i), salesOrderLines.get(j));
                    if (result == 0 && salesOrderLines.get(i).getBoxNumber().equals(salesOrderLines.get(j).getBoxNumber())) {
                        if (!salesOrderLines.get(j).getSerialNumber().equals("")) {
                            salesOrderLines.get(i).getSerialNumbers().add(salesOrderLines.get(j).getSerialNumber());
                        }
                        salesOrderLines.get(i).setQuantity(salesOrderLines.get(i).getQuantity() + salesOrderLines.get(j).getQuantity());
                        salesOrderLines.remove(j);
                    }
                }
            }
            
            for (SalesOrderLine s : salesOrderLines) {
                s.setGrossWeight(Math.round((float)s.getGrossWeight() / s.getQuantity()));
            }
            for (int i = 0; i < salesOrderLines.size(); i++) {
                for (int j = salesOrderLines.size() - 1; j > i; j--) {
                    int result = CSVFileReader.compare(salesOrderLines.get(i), salesOrderLines.get(j));
                    if (result == 0) {
                        if(!salesOrderLines.get(j).getSerialNumber().equals("")) {
                            salesOrderLines.get(i).getSerialNumbers().addAll(salesOrderLines.get(j).getSerialNumbers());
                        }    
                        salesOrderLines.get(i).getBoxNumbers().add(salesOrderLines.get(j).getBoxNumber());
                        salesOrderLines.get(i).setBoxNumber(null);
                        salesOrderLines.get(i).setQuantity(salesOrderLines.get(i).getQuantity() + salesOrderLines.get(j).getQuantity());
                        salesOrderLines.get(i).setGrossWeight((salesOrderLines.get(i).getGrossWeight() + salesOrderLines.get(j).getGrossWeight()) / 2);
                        salesOrderLines.remove(j);
                    }
                }
            }

            csvFileReader.salesOrders = new ArrayList<SalesOrder>();
            for (int i = 0; i < salesOrderLines.size(); i++) {
                SalesOrder sOrder = new SalesOrder(salesOrderLines.get(i).getPurchaseOrderNumber(), salesOrderLines.get(i).getSalesOrderNumber());
                sOrder.getLines().add(salesOrderLines.get(i));
                csvFileReader.salesOrders.add(sOrder);
                for (int j = salesOrderLines.size() - 1; j > i; j--) {
                    if (salesOrderLines.get(j).getSalesOrderNumber().equals(salesOrderLines.get(i).getSalesOrderNumber())
                        && salesOrderLines.get(j).getPurchaseOrderNumber().equals(salesOrderLines.get(i).getPurchaseOrderNumber())) {
                            sOrder.getLines().add((salesOrderLines.get(j)));
                            salesOrderLines.remove(j);
                    }
                }
            }

            salesOrderLines = null;


        } catch (IOException ex) {
            ex.printStackTrace();
        } 
        return csvFileReader.salesOrders;
    }

    public static int defineIndex(String[] array, String str) {
        int index = -1;
        for (int p = 0; p < array.length; p++) {
            if (array[p].equals(str)) {
                index = p;
                break;
            }
        }
        return index;
    }

    public static int compare(SalesOrderLine l1, SalesOrderLine l2) {
        int i;
        i = l1.getPurchaseOrderNumber().compareTo(l2.getPurchaseOrderNumber());
        if (i == 0) {
            i = l1.getSalesOrderNumber().compareTo(l2.getSalesOrderNumber());
            if (i == 0) {
                i = l1.getShipmentId().compareTo(l2.getShipmentId());
                if (i == 0) {
                    i = l1.getPartNumber().compareTo(l2.getPartNumber());
                    if (i == 0) {
                        i = l1.getInvoiceId().compareTo(l2.getInvoiceId());

                    }
                }
            }
        }
        return i;
    }
}
